package com.github.nuggets.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.nuggets.model.entity.NuggetsAccountEntity;
import com.github.nuggets.repository.AccountRepository;
import com.github.nuggets.repository.db.NuggetsAccountMapper;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/17 6:40 下午
 */
@Service
public class AccountRepositoryImpl extends ServiceImpl<NuggetsAccountMapper, NuggetsAccountEntity> implements AccountRepository {


    @Override
    public Optional<NuggetsAccountEntity> getByUserName(String userName) {

        NuggetsAccountEntity account = getOne(new LambdaQueryWrapper<NuggetsAccountEntity>()
                .eq(NuggetsAccountEntity::getUserName, userName)
        );
        return Optional.ofNullable(account);
    }

    @Override
    public Optional<NuggetsAccountEntity> getAccountInfoByUserName(String userName) {

        NuggetsAccountEntity account = getOne(
                new LambdaQueryWrapper<NuggetsAccountEntity>()
                .select(NuggetsAccountEntity::getId,
                        NuggetsAccountEntity::getUserName,
                        NuggetsAccountEntity::getNickName,
                        NuggetsAccountEntity::getPassword,
                        NuggetsAccountEntity::getTelephone,
                        NuggetsAccountEntity::getEmail,
                        NuggetsAccountEntity::getRole)
                .eq(NuggetsAccountEntity::getUserName,userName)
        );
        return Optional.ofNullable(account);
    }

    @Override
    public Boolean isUserNameExist(String userName) {

        Optional<NuggetsAccountEntity> account = Optional.ofNullable(
                //根据userName查找
                getOne(new LambdaQueryWrapper<NuggetsAccountEntity>()
                        .select(NuggetsAccountEntity::getUserName)
                        .eq(NuggetsAccountEntity::getUserName, userName)
                )
        );
        return account.isPresent();
    }

}
